package com.studio.bill.swiperecyclerdemo.util;

import android.content.Context;
import android.widget.Toast;

import com.studio.bill.swiperecyclerdemo.MainApplication;

public abstract class DefaultHttpRequest {
    protected Context context;

    public DefaultHttpRequest() {
        context = MainApplication.getInstance();
    }

    public DefaultHttpRequest(Context activity) {
        context = activity;
    }

    public abstract void onSuccess(String json);

    public void onFaliled(ErrorCode error) {}

    public RequestParams params() {
        return new RequestParams();
    }

    public abstract String url();

    protected boolean showNetworkError() {
        return true;
    }

    protected void commonAlert(ErrorCode errorCode) {
        if (errorCode.errorCode / 100 == 2) {
            // status code 2xx, do nothing
        } else if (errorCode.errorCode / 100 == 4) {
            toast("资源不可用");
        } else if (errorCode.errorCode / 100 == 5) {
            toast("服务不可用");
        } else {
            if (!Tools.isNetworkAvailable(context)) {
                toast("网络不可用");
            } else {
                toast("未知错误");
            }
        }
    }

    protected void toast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

}
